﻿using System;
using System.Data;
using System.Transactions;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skyworth.CRM.DAL
{
    public class CustCreditDAL : SuperDataBase, ICustCreditDAL
    {
        public void Add(CustCredit model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_Cust_Credit(");
                strSql.Append("FCreditType,FCustId,FType,FBeginDate,FEndDate,FAmount,FCreditWay,FYearAmount,FisYes)");
                strSql.Append(" values (");
                strSql.Append("@FCreditType,@FCustId,@FType,@FBeginDate,@FEndDate,@FAmount,@FCreditWay,@FYearAmount,@FisYes)");
                //strSql.Append(";select @@IDENTITY");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FCreditType", DbType.Int32, model.FCreditType);
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FType", DbType.Int32, model.FType);
                DB.AddInParameter(cmd, "@FBeginDate", DbType.DateTime, model.FBeginDate);
                DB.AddInParameter(cmd, "@FEndDate", DbType.DateTime, model.FEndDate);
                DB.AddInParameter(cmd, "@FAmount", DbType.Decimal, model.FAmount);
                DB.AddInParameter(cmd, "@FCreditWay", DbType.Int32, model.FCreditWay);
                DB.AddInParameter(cmd, "@FYearAmount", DbType.Decimal, model.FYearAmount);
                DB.AddInParameter(cmd, "@FisYes", DbType.Boolean, model.FisYes);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(CustCredit model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Cust_Credit set ");
                strSql.Append("FCreditType=@FCreditType,");
                strSql.Append("FCustId=@FCustId,");
                strSql.Append("FType=@FType,");
                strSql.Append("FBeginDate=@FBeginDate,");
                strSql.Append("FEndDate=@FEndDate,");
                strSql.Append("FAmount=@FAmount,");
                strSql.Append("FCreditWay=@FCreditWay,");
                strSql.Append("FYearAmount=@FYearAmount,");
                strSql.Append("FisYes=@FisYes");
                strSql.Append(" where FId=@FId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FId", DbType.Int32, model.FId);
                DB.AddInParameter(cmd, "@FCreditType", DbType.Int32, model.FCreditType);
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FType", DbType.Int32, model.FType);
                DB.AddInParameter(cmd, "@FBeginDate", DbType.DateTime, model.FBeginDate);
                DB.AddInParameter(cmd, "@FEndDate", DbType.DateTime, model.FEndDate);
                DB.AddInParameter(cmd, "@FAmount", DbType.Decimal, model.FAmount);
                DB.AddInParameter(cmd, "@FCreditWay", DbType.Int32, model.FCreditWay);
                DB.AddInParameter(cmd, "@FYearAmount", DbType.Decimal, model.FYearAmount);
                DB.AddInParameter(cmd, "@FisYes", DbType.Boolean, model.FisYes);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int id)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from T_Cust_Credit ");
                strSql.Append(" where FId=@FId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FId", DbType.Int32, id);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public CustCredit GetModel(int id)
        {
            try
            {
                CustCredit model = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 FId,FCreditType,FCustId,FType,FBeginDate,FEndDate,FAmount,FCreditWay,FYearAmount,FisYes from T_Cust_Credit ");
                strSql.Append(" where FId=@FId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FId", DbType.Int32, id);
                using (IDataReader dr = DB.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        model = new CustCredit();
                        if (dr["FId"].ToString() != "")
                        {
                            model.FId = int.Parse(dr["FId"].ToString());
                        }
                        if (dr["FCreditType"].ToString() != "")
                        {
                            model.FCreditType = int.Parse(dr["FCreditType"].ToString());
                        }
                        if (dr["FCustId"].ToString() != "")
                        {
                            model.FCustId = int.Parse(dr["FCustId"].ToString());
                        }
                        if (dr["FType"].ToString() != "")
                        {
                            model.FType = int.Parse(dr["FType"].ToString());
                        }                        
                        if (dr["FBeginDate"].ToString() != "")
                        {
                            model.FBeginDate = DateTime.Parse(dr["FBeginDate"].ToString());
                        }
                        if (dr["FEndDate"].ToString() != "")
                        {
                            model.FEndDate = DateTime.Parse(dr["FEndDate"].ToString());
                        }
                        if (dr["FAmount"].ToString() != "")
                        {
                            model.FAmount = decimal.Parse(dr["FAmount"].ToString());
                        }
                        if (dr["FCreditWay"].ToString() != "")
                        {
                            model.FCreditWay = int.Parse(dr["FCreditWay"].ToString());
                        }

                        if (dr["FYearAmount"].ToString() != "")
                        {
                            model.FYearAmount = decimal.Parse(dr["FYearAmount"].ToString());
                        }
                        if (dr["FisYes"].ToString() != "")
                        {
                            if ((dr["FisYes"].ToString() == "1") || (dr["FisYes"].ToString().ToLower() == "true"))
                            {
                                model.FisYes = true;
                            }
                            else
                            {
                                model.FisYes = false;
                            }
                        }
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Query(string strWhere)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select FId,FCreditType,case FCreditType when 1 then '为销售信用' when 2 then '为财务信用' else '为业务员信用' end as FCreditName,");
                strSql.Append("FCustId,tbc.FNumber as FCustCode,tbc.FName as FCustName,FType,case FType when 1 then '是追加信用额度' else '是扣减信用额度' end as TypeName,");
                strSql.Append("FBeginDate,FEndDate,FAmount,FCreditWay,case FCreditWay when 1 then '按比率授信' else '' end as WayName,");
                strSql.Append("FYearAmount,FisYes FROM T_Cust_Credit as tcc,T_BasicData_Customer as tbc");
                strSql.Append(" where tcc.FCustId=tbc.FItemId");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(strWhere);
                }
                DataSet ds = DB.ExecuteDataSet(CommandType.Text, strSql.ToString());
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}